package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.bdt.api.base.PortApi;
import com.cmc.cloud.cmclink.bdt.api.base.dto.req.PortListByCodeReqDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.req.PortMultiFieldFuzzyQueryReqDTO;
import com.cmc.cloud.cmclink.bdt.api.base.dto.resp.PortListByCodeRespDTO;
import com.cmc.cloud.cmclink.doc.rpcservice.PortNameTransferService;
import com.cmc.cloud.cmclink.doc.rpcservice.vo.PortNameBaseVo;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 周贤
 * @date 2025/3/31
 */
@Service
@Validated
public class PortNameTransferServiceImpl implements PortNameTransferService {

    @Resource
    private PortApi portApi;

    @Override
    public List<String> addQuery(String keyWord) {
        PortMultiFieldFuzzyQueryReqDTO reqDTO = new PortMultiFieldFuzzyQueryReqDTO();
        reqDTO.setCharacter(keyWord);
        CommonResult<List<PortListByCodeRespDTO>> listCommonResult = portApi.multiFieldFuzzyQuery(reqDTO);
        if(listCommonResult!=null && CollectionUtil.isNotEmpty(listCommonResult.getData())){
            List<String> codeList = listCommonResult.getCheckedData().stream().map(PortListByCodeRespDTO::getPortCode).collect(Collectors.toList());
          return codeList;
        }
        return Collections.emptyList();
    }

    @Override
    public void fillName(List<? extends PortNameBaseVo> list) {
        List<String> portCodeList = list.stream().map(PortNameBaseVo::getPortCode).collect(Collectors.toList());
        PortListByCodeReqDTO portListByCodeReqDTO = new PortListByCodeReqDTO();
        portListByCodeReqDTO.setPortCodeList(portCodeList);
        CommonResult<List<PortListByCodeRespDTO>> listCommonResult = portApi.listByCodeList(portListByCodeReqDTO);
        Map<String, String> cnMap = listCommonResult.getCheckedData().stream().collect(Collectors.toMap(PortListByCodeRespDTO::getPortCode, PortListByCodeRespDTO::getPortNameCn));
        Map<String, String> enMap = listCommonResult.getCheckedData().stream().collect(Collectors.toMap(PortListByCodeRespDTO::getPortCode, PortListByCodeRespDTO::getPortNameEn));
        list.forEach(vo -> {
            vo.setPortNameEn(enMap.get(vo.getPortCode()));
            vo.setPortNameLocal(cnMap.get(vo.getPortCode()));
        });
    }
}
